home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / secondary.c < prev    next >
C/C++ Source or Header  |  1996-07-27  |  4KB  |  157 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <limits.h>
  5. #include <sys/stat.h>
  6. #include "internal.h"
  7. #include "dnsconf.h"
  8. #include "dnsconf.m"
  9.  
  10. static DNSCONF_HELP_FILE help_secondary("secondary");
  11.  
  12.  
  13. PUBLIC SECONDARY::SECONDARY(
  14.     const char *_domain,
  15.     const char *_backup,
  16.     const char **_ip_addr,
  17.     int nb_ip)
  18. {
  19.     domain.setfrom (_domain);
  20.     backup.setfrom (_backup);
  21.     for (int i=0; i<nb_ip; i++) addr[i].setfrom(_ip_addr[i]);
  22. }
  23.  
  24. PUBLIC SECONDARY::SECONDARY()
  25. {
  26. }
  27.  
  28. PUBLIC void SECONDARY::write(FILE *fout, const char *named_dir) const
  29. {
  30.     fprintf (fout,"secondary\t%s",domain.get());
  31.     for (int i=0; i<4; i++){
  32.         fprintf (fout," %s",addr[i].get());
  33.     }
  34.     if (backup.is_empty){
  35.         /* #Specification: secondary / backup file / location
  36.             dnsconf set the backup file in the subdirectory
  37.             "sec" of the DNS configuration directory.
  38.             It simply use the domain as the name of the file.
  39.  
  40.             You can override this by editing /etc/named.boot by
  41.             hand. dnsconf will preserve the new path.
  42.  
  43.             The "sec" subdirectory will be created automaticly.
  44.         */
  45.         char path[PATH_MAX];
  46.         sprintf (path,"%s/sec",named_dir);
  47.         mkdir (path,0755);
  48.         fprintf (fout,"\tsec/%s\n",domain.get());
  49.     }else{
  50.         fprintf (fout,"\t%s\n",backup.get());
  51.     }
  52. }
  53.  
  54.  
  55. /*
  56.     Edit the minimal spec so this dns will act as a secondary for another
  57.     Return -1 if the user escaped away.
  58.     Return  0 if the changes are accepted
  59.     Return  1 if the user wish to delete this entry
  60. */
  61. PUBLIC int SECONDARY::edit()
  62. {
  63.     DIALOG dia;
  64.     dia.newf_str (MSG_R(F_DOMAIN),domain);
  65.     dia.newf_str (MSG_U(F_IPSERV,"IP address of the server"),addr[0]);
  66.     dia.newf_str ("",addr[1]);
  67.     dia.newf_str ("",addr[2]);
  68.     dia.newf_str ("",addr[3]);
  69.     int ret = -1;
  70.     int nof = 0;
  71.     while (1){
  72.         MENU_STATUS status = dia.edit (
  73.              MSG_U(T_SECSPEC,"Secondary specification")
  74.             ,MSG_U(I_SECSPEC
  75.              ,"You must enter a domain and at least IP address\n"
  76.               "of one DNS server of that domain\n")
  77.             ,help_secondary.getpath()
  78.             ,nof
  79.             ,MENUBUT_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL);
  80.         if (status == MENU_CANCEL || status == MENU_ESCAPE){
  81.             break;
  82.         }else if (status == MENU_DEL){
  83.             if (xconf_areyousure(MSG_U(Q_DELSECONDARY
  84.                 ,"Confirm deletion of a secondary spec"))){
  85.                 ret = 1;
  86.                 break;
  87.             }
  88.         }else{
  89.             if (domain.is_empty()
  90.                 || addr[0].is_empty()){
  91.                 xconf_error(MSG_U(E_FILLDOM
  92.                     ,"Fill at least the domain\n"
  93.                      "and the first IP address"));
  94.             }else{
  95.                 ret = 0;
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.     if (ret != 0) dia.restore();
  101.     return ret;
  102. }
  103.  
  104. PUBLIC SECONDARY *SECONDARYS::getitem (int no) const
  105. {
  106.     return (SECONDARY*)ARRAY::getitem(no);
  107. }
  108.  
  109. PUBLIC void SECONDARYS::write (FILE *fout, const char *named_dir) const
  110. {
  111.     for (int i=0; i<getnb(); i++) getitem(i)->write(fout,named_dir);
  112. }
  113.  
  114. PUBLIC void SECONDARYS::edit(DNS &dns)
  115. {
  116.     int choice=0;
  117.     while (1){
  118.         int nb = getnb();
  119.         const char **menuopt = (const char**)malloc((nb*2+1)*sizeof(char*));
  120.         int ii=0;
  121.         for (int i=0; i<nb; i++){
  122.             menuopt[ii++] = " ";
  123.             menuopt[ii++] = getitem(i)->domain.get();
  124.         }
  125.         menuopt[ii] = NULL;
  126.         MENU_STATUS code = xconf_menu (MSG_U(T_SECONDARYS,"Secondarys")
  127.             ,MSG_U(I_SECONDARYS
  128.                 ,"You are allowed to edit/add/remove secondaries\n")
  129.             ,help_secondary
  130.             ,NULL
  131.             ,NULL
  132.             ,NULL
  133.             ,MSG_U(I_ADDSEC,"add one secondary spec")
  134.             ,menuopt,choice);
  135.         free (menuopt);
  136.         if (code == MENU_QUIT || code == MENU_ESCAPE){
  137.             break;
  138.         }else if (code == MENU_ADD){
  139.             SECONDARY *sec = new SECONDARY;
  140.             if (sec->edit()==0){
  141.                 add (sec);
  142.                 dns.write();
  143.             }else{
  144.                 delete sec;
  145.             }
  146.         }else{
  147.             SECONDARY *sec = getitem(choice);
  148.             int ok = sec->edit();
  149.             if (ok != -1){
  150.                 if (ok == 1) remove_del (sec);
  151.                 dns.write();
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157.